package com.songcha.library_business.proxy.database

import com.songcha.library_business.helper.DaoManagerHelper
import com.songcha.library_business.proxy.userinfo.UserInfoManager
import com.songcha.library_common.util.LogUtil
import com.songcha.library_database_douyue.bean.BookReadProgressDBBean
import com.songcha.library_database_douyue.greendao.BookReadProgressDBBeanDao
import com.songcha.library_database_douyue.greendao.BookShelfDBBeanDao
import com.songcha.library_database_douyue.manager.GreenDaoManager
import org.greenrobot.greendao.query.WhereCondition


class BookReadProgressDaoManager {
    companion object{

        @JvmStatic
        fun saveReadProgress(bookId:String, currentChapter:Int,beginPosition:Int,endPosition:Int,chapterSize:Int){
            if(bookId.isBlank() && currentChapter<1) return
            try{
                val dao=GreenDaoManager.getInstance().bookReadProgressDBBeanDao
                val bean=BookReadProgressDBBean().apply {
                    this.bookId=bookId
                    this.currentChapter=currentChapter
                    this.beginPosition=beginPosition
                    this.endPosition=endPosition
                    this.userId=UserInfoManager.userId
                    this.chapterSize=chapterSize
                }
                val qb=dao.queryBuilder()
                    .where(BookReadProgressDBBeanDao.Properties.BookId.eq(bookId))
                    .where(BookReadProgressDBBeanDao.Properties.UserId.eq(UserInfoManager.userId))
                val list=qb.list()
                if(list.size==0){
                    dao.insert(bean)
                }else{
                    bean.id=list[0].id
                    dao.update(bean)
                }
            }catch (e:Exception){
                LogUtil.log("保存阅读记录异常")
                e.printStackTrace()
            }
        }

        @JvmStatic
        fun getReadProgress(bookId:String):IntArray{
            if(bookId.isBlank()) return intArrayOf(1,0,0)
            val dao=GreenDaoManager.getInstance().bookReadProgressDBBeanDao
            val qb=dao.queryBuilder()
                .where(BookReadProgressDBBeanDao.Properties.BookId.eq(bookId))
                .where(BookReadProgressDBBeanDao.Properties.UserId.eq(UserInfoManager.userId))
            val list=qb.list()
            if(list.size>0){
                val bean=list[0]
                if(bean.currentChapter<1) bean.currentChapter=1
                return intArrayOf(bean.currentChapter,bean.beginPosition,bean.endPosition)
            }
            return intArrayOf(1,0,0)
        }

        @JvmStatic
        fun getReadProgressListByIdList(ids: MutableList<String>):MutableList<BookReadProgressDBBean>{
            if(ids.size==0) return mutableListOf()
            val dao=GreenDaoManager.getInstance().bookReadProgressDBBeanDao
            val qb=dao.queryBuilder()
            qb.where(BookReadProgressDBBeanDao.Properties.UserId.eq(UserInfoManager.userId))
                .where(
                    WhereCondition.StringCondition(
                        BookShelfDBBeanDao.Properties.BookId.columnName + " in " + DaoManagerHelper.getRangeStringByIds(
                            ids
                        ) + " order by case " + BookShelfDBBeanDao.Properties.BookId.columnName + DaoManagerHelper.getWhenCaseStringByIds(
                            ids
                        ) + "end"
                    )
                )
                //.where(WhereCondition.StringCondition(BookReadProgressDBBeanDao.Properties.BookId.columnName + " in " + DaoManagerHelper.getRangeStringByIds(ids)))
            val list= qb.list()
            //may be not exist in progress db,so need to supplement
            if(list.size==ids.size)
                return list
            else{
                val tmp= mutableListOf<BookReadProgressDBBean>()
                for(i in 0 until ids.size){
                    val item=ids[i]
                    var bean=getBookReadProgressById(list,item)
                    if(bean==null){
                        bean= BookReadProgressDBBean()
                        bean!!.bookId=item
                        bean!!.userId=UserInfoManager.userId
                        bean!!.currentChapter=0
                    }
                    tmp.add(bean)
                }
                return tmp
            }

        }

        @JvmStatic
        private fun getBookReadProgressById(list:MutableList<BookReadProgressDBBean>,id:String):BookReadProgressDBBean?{
            for(item in list){
                if(item.bookId==id)
                    return item
            }
            return null
        }

    }
}